class media {
  peer: any;
  init(param: any) {
    this.peer = param.peer;
    let self = this;
    this.peer.ontrack = function(evt: any) {
      //const localVideo = document.getElementById("remoteVideo");
      //(localVideo as any).srcObject = evt.streams[0];
    };
    console.log("------------------111-----------------------");
    setTimeout(() => {
      console.log("开始 调用摄像头 start...");
      self.start_test();
    }, 10000);
  }

  start_test() {
    var p = navigator.mediaDevices.getUserMedia({ audio: false, video: true });

    p.then(function(mediaStream) {
      //var video = document.querySelector("video");
      console.log("-------------video-------------------");
      const video = document.getElementById("mediaVideo") as any;
      video.src = window.URL.createObjectURL(mediaStream);
      video.onloadedmetadata = function(e) {
        // Do something with the video here.
      };
    });

    p.catch(function(err) {
      console.log(err.name);
    }); // always check for errors at the end.
  }

  getMedia() {
    navigator.getUserMedia =
      navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
    return !!navigator.getUserMedia;
  }
  async start() {
    let self = this;

    const localVideo = document.getElementById("mediaVideo");
    //let media = this.getMedia();

    var constraints = {
      audio: true,
      video: true
    };

    function successCallback(stream: any) {
      (window as any).stream = stream; // stream available to console
      if (window.URL) {
        (localVideo as any).src = window.URL.createObjectURL(stream);
      } else {
        (localVideo as any).src = stream;
      }
    }

    function errorCallback(error: any) {
      console.log("navigator.getUserMedia error: ", error);
    }
    navigator.mediaDevices
      .getUserMedia({
        audio: true,
        video: true
      })
      .then(mediaStream => {
        //(localVideo as any).srcObject = mediaStream;
        console.log("9999999999999:", mediaStream);
        //(localVideo as any).onloadedmetadata = e => (localVideo as any).play();
        // var options = { mimeType : 'video/quicktime'};
      });

    /*
    navigator.mediaDevices
      .getUserMedia({
        video: true
      }) 
      .then(
        stream => ((localVideo as any).srcObject = stream),
        err => console.log(err)
      );
      */
    /*
    try {
      let stream = await navigator.mediaDevices.getUserMedia(constraints);
      if (window.URL) {
        (localVideo as any).src = window.URL.createObjectURL(stream);
      } else {
        (localVideo as any).src = stream;
      }
    } catch (e) {
      console.log("e :", e);
    }
    */

    /*
    navigator.mediaDevices
      .getUserMedia({
        video: true,
        audio: true
      })
      .then(function(stream) {
        /* 使用这个stream stream */
    /*   (localVideo as any).src = stream;
        console.log("----------------------------");
        stream.getTracks().forEach(track => {
          self.peer.addTrack(track, stream);
        });
      })
      .catch(function(err) {
        /* 处理error */
    //    console.log("media err:", err);
    //});
    // */
    /*
    (localVideo as any).src = mediaStream;
    console.log("----------------------------");
    mediaStream.getTracks().forEach(track => {
      self.peer.addTrack(track, mediaStream);
    });
    */
  }
}

export default new media();
